package org.westboy.common.Listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;
import org.westboy.common.constant.Const;

import java.io.File;

@Component
public class ApplicationEventListener implements ApplicationListener {

    private static Logger logger = LoggerFactory.getLogger(ApplicationEventListener.class);


    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            logger.info(">>>>>>>>>>>>>>>>初始化环境变量");
        } else if (event instanceof ApplicationPreparedEvent) {
            logger.info(">>>>>>>>>>>>>>>>初始化完成");
        } else if (event instanceof ContextRefreshedEvent) {
            logger.info(">>>>>>>>>>>>>>>>应用刷新");
        } else if (event instanceof ApplicationReadyEvent) {
            logger.info(">>>>>>>>>>>>>>>>应用已启动完成");
            //创建配置文件目录
            File dir = new File(Const.CONFIG_FOLDER);
            if(!dir.exists()){
                logger.info(">>>>>>>>>>>>>>>>创建数据存储目录");
                dir.mkdirs();
            }
        } else if (event instanceof ContextStartedEvent) {
            logger.info(">>>>>>>>>>>>>>>>应用启动，需要在代码动态添加监听器才可捕获");
        } else if (event instanceof ContextStoppedEvent) {
            logger.info(">>>>>>>>>>>>>>>>应用停止");
        } else if (event instanceof ContextClosedEvent) {
            logger.info(">>>>>>>>>>>>>>>>应用关闭");
        }
    }


}

